package com.woniuxy.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.woniuxy.dto.OrderDTO;
import com.work.framework.pojo.*;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.ietf.jgss.Oid;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author: wanghao
 * @Date: 2019/9/9 22:42
 */
@Mapper
@Repository
public interface OrderMapper extends BaseMapper<Order> {

    /**
     * 拍卖成功后添加订单
     *
     * @param order（订单号，买家ID，商品ID，成交价，手续费，订单创建时间）
     * @return 添加订单状态
     */
    @Insert("INSERT INTO `order` (orderNum,bid,gid,transactionPerice,charges,buildTime)VALUES(#{orderNum},#{bid},#{gid},#{transactionPerice},#{charges},#{buildTime}) ")
    Integer insertOrder(Order order);

    /**
     * 将订单状态修改为0.删除 1.未完成 2.过期 3.成交 4.支付未完成
     *
     * @param oid
     * @param flage
     * @return 修改订单状态操作是否成功
     */
    @Update("UPDATE `order` SET flage =#{flage} WHERE oid = #{oid};")
    Integer updateOrder(Integer oid, Integer flage);

    /**
     * 买家订单信息
     *
     * @param orderDTO
     * @return
     */
    @Select("SELECT oid,expressNum,orderNum,bid,uname AS bname,goodsVIEW.*,sid,transactionPerice,charges,buildTime,paymentTime,dealTime,flage FROM goodsVIEW,`order`,user_info " +
            "WHERE `order`.gid = goodsVIEW.gid AND user_info.uid = `order`.bid and flage != 0 and bid = #{bid}  order by oid desc")
    List<OrderDTO> searchBayerOrderDTO(OrderDTO orderDTO);

    /**
     * 卖家订单信息
     *
     * @param orderDTO
     * @return
     */
    @Select("SELECT oid,expressNum,orderNum,bid,uname AS bname,goodsVIEW.*,sid,transactionPerice,charges,buildTime,paymentTime,dealTime,flage FROM goodsVIEW,`order`,user_info " +
            "WHERE `order`.gid = goodsVIEW.gid AND user_info.uid = `order`.bid and flage != 0 and vid = #{vid} order by oid desc")
    List<OrderDTO> searchSellerOrderDTO(OrderDTO orderDTO);

    /**
     * 单条信息查询
     *
     * @param oid
     * @return
     */
    @Select("SELECT oid,expressNum,orderNum,bid,uname AS bname,goodsVIEW.*,sid,transactionPerice,charges,buildTime,paymentTime,dealTime,flage FROM goodsVIEW,`order`,user_info " +
            "WHERE `order`.gid = goodsVIEW.gid AND user_info.uid = `order`.bid and flage != 0 and oid = #{oid}")
    OrderDTO findOrderDTOByOid(Integer oid);

    /**
     * 单条信息查询
     *
     * @param oid
     * @return
     */
    @Select("select * from `order` where oid = #{oid}")
    Order findOrderByOid(Integer oid);

    /**
     * 添加支付时间
     *
     * @param payTime
     * @param oid
     */
    @Update("update `order` set paymentTime = #{payTime},sid =#{sid} where oid=#{oid}")
    void updatePayTime(String payTime, Integer sid, Integer oid);

    /**
     * 添加快递单号
     *
     * @param expressNum
     * @return
     */
    @Update("UPDATE `order` set expressNum = #{expressNum} where oid =#{oid} ")
    Integer addExpressNum(String expressNum, Integer oid);

    /**
     * 后台订单信息
     * @param orderDTO
     * @return
     */
    @Select("SELECT oid,expressNum,orderNum,bid,uname AS bname,goodsVIEW.*,sid,transactionPerice,charges,buildTime,paymentTime,dealTime,flage FROM goodsVIEW,`order`,user_info " +
            "WHERE `order`.gid = goodsVIEW.gid AND user_info.uid = `order`.bid and flage != 0 and orderNum like #{orderNum} and gname like #{gname} and cname like #{cname} order by oid desc")
    List<OrderDTO> searchOrderDTO(OrderDTO orderDTO);

    /**
     * 插入完成时间
     * @param dealTime
     */
    @Insert("UPDATE `order` set dealTime = #{dealTime} where oid =#{oid} ")
    void insertdealTime(String dealTime);
}
